/**
 * Created by 白雨浓 on 17-9-4 上午11:25.
 *
 * 消息提示统一处理封装
 */

const Notify = {
  install(Vue){

    const msg = {
      success(msg){
        Vue.prototype.$message({message: msg, type: 'success'});
      },
      warning(msg){
        Vue.prototype.$message({message: msg, type: 'warning'});
      },
      info(msg){
        Vue.prototype.$message({message: msg, type: 'info'});
      },
      error(msg){
        Vue.prototype.$message({message: msg, type: 'error'});
      },
    };

    const notify = {
      success(title, msg) {
        Vue.prototype.$notify({
          title: title,
          message: msg,
          type: 'success'
        });
      },

      warning(title, msg) {
        Vue.prototype.$notify({
          title: title,
          message: msg,
          type: 'warning'
        });
      },

      info(title, msg) {
        Vue.prototype.$notify.info({
          title: title,
          message: msg,
          type: 'info'
        });
      },

      error(title, msg) {
        Vue.prototype.$notify({
          title: title,
          message: msg,
          type: 'error'
        });
      }
    };

    const confirm = {
      confirm(title, msg, callback){
        Vue.prototype.$confirm(`${msg}`, `${title}`, {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          closeOnClickModal: true,
          closeOnPressEscape: true,
          type: 'warning',
          beforeClose: (action, instance, done) => {
            if (action === 'cancel') {
              if (instance.confirmButtonLoading)
                return false;
              instance.confirmButtonLoading = false;
              done();
              return false;
            }
            instance.closeOnClickModal = false;
            instance.closeOnPressEscape = false;
            instance.confirmButtonLoading = true;
            instance.confirmButtonText = '提交中...';
            callback(() => {
              instance.confirmButtonLoading = false;
              done();
            });
          }
        })
      },
      delete(name, callback){
        Vue.prototype.$confirm(`此操作将永久删除（${name}）, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          closeOnClickModal: true,
          closeOnPressEscape: true,
          type: 'warning',
          beforeClose: (action, instance, done) => {
            if (action === 'cancel') {
              if (instance.confirmButtonLoading)
                return false;
              instance.confirmButtonLoading = false;
              done();
              return false;
            }
            instance.closeOnClickModal = false;
            instance.closeOnPressEscape = false;
            instance.confirmButtonLoading = true;
            instance.confirmButtonText = '删除中...';
            callback(() => {
              instance.confirmButtonLoading = false;
              done();
            });
          }
        })
      }
    };

    Vue.prototype.$$msg = msg;
    Vue.prototype.$$notify = notify;
    Vue.prototype.$$confirm = confirm;
  }
};

export default Notify;
